package com.edu.zju.scs.panacea.service.plan;

import com.edu.zju.scs.panacea.entity.BaseResult;
import com.edu.zju.scs.panacea.entity.plan.Industry;
import com.edu.zju.scs.panacea.mapper.plan.IndustryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: Yue Liang
 * @Date: 2018-12-24 10:33
 */
@Service
public class IndustryService {

    @Autowired
    private IndustryMapper industryMapper;

    /**
     * 根据ID查询某个行业信息
     * @param id
     * @return
     */
    public BaseResult getIndustry(Long id) {
        BaseResult baseResult = new BaseResult();
        Industry industry = industryMapper.selectIndustry(id);
        if (industry == null) {
            baseResult.error("无此id对应的行业");
        } else {
            baseResult.success(industry);
        }
        return baseResult;
    }



    /**
     * 查询所有行业信息
     * @return
     */
    public BaseResult getAllIndustry() {
        BaseResult baseResult = new BaseResult();
        List<Industry> industryList = industryMapper.selectAllIndustry();
        if (industryList == null) {
            baseResult.error("查询行业信息失败");
        } else {
            baseResult.success(industryList);
        }
        return baseResult;
    }


    public BaseResult deleteIndustry(Long id) {
        BaseResult baseResult = new BaseResult();
        long deleteDevNums = industryMapper.deleteIndustry(id);
        if (deleteDevNums == 0) {
            baseResult.error("删除行业失败！");
        } else {
            baseResult.success(deleteDevNums);
        }
        return baseResult;
    }

    public BaseResult addIndustry(Industry industry) {
        BaseResult baseResult = new BaseResult();
        // 返回插入记录条数
        Long insertNums = industryMapper.insertIndustry(industry);
        if (insertNums == null) {
            baseResult.error("新增行业失败！");
        } else {
            baseResult.success(insertNums);
        }
        return baseResult;
    }

    public BaseResult editIndustry(Industry industry) {
        BaseResult baseResult = new BaseResult();
        // 返回更新的记录条数
        Long updateNums = industryMapper.updateIndustry(industry);
        if (updateNums == null) {
            baseResult.error("更新行业失败！");
        } else {
            baseResult.success(updateNums);
        }
        return baseResult;
    }
}
